﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Linq;

namespace suirui.ZhuMu.WebCore
{
    /// <summary>
    /// 隐藏接口，不生成到swagger文档展示
    /// </summary>
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]

    public partial class HiddenApiAttribute : Attribute { }
    public class HiddenApiFilter : Swashbuckle.AspNetCore.SwaggerGen.IDocumentFilter
    {
        ///// <summary>
        ///// 重写Apply方法，移除隐藏接口的生成
        ///// </summary>
        ///// <param name="swaggerDoc">swagger文档文件</param>
        ///// <param name="schemaRegistry"></param>
        ///// <param name="apiExplorer">api接口集合</param>
        //public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        //{
        //    foreach (var apiDescription in apiExplorer.ApiDescriptions)
        //    {
        //        if (Enumerable.OfType<HiddenApiAttribute>(apiDescription.GetControllerAndActionAttributes<HiddenApiAttribute>()).Any())
        //        {
        //            string key = "/" + apiDescription.RelativePath;
        //            if (key.Contains("?"))
        //            {
        //                int idx = key.IndexOf("?", StringComparison.Ordinal);
        //                key = key.Substring(0, idx);
        //            }
        //            swaggerDoc.paths.Remove(key);
        //        }
        //    }
        //}

        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            foreach (var apiDescription in context.ApiDescriptions)
            {
                if (Enumerable.OfType<HiddenApiAttribute>(apiDescription.CustomAttributes()).Any())
                {
                    string key = "/" + apiDescription.RelativePath;
                    if (key.Contains("?"))
                    {
                        int idx = key.IndexOf("?", StringComparison.Ordinal);
                        key = key.Substring(0, idx);
                    }
                    swaggerDoc.Paths.Remove(key);
                }
            }
        }
    }
}
